package modelLayer;
import java.util.ArrayList;
import java.util.Scanner;
public class DvdContainer
{
    private ArrayList<DVD> myDvdContainer;    
    private static DvdContainer  instance;
    private DvdContainer()
    {
        myDvdContainer = new ArrayList<DVD>();
    }
    /** 
       opretter en container hvis der ikke findes en
       */
    public static DvdContainer getInstance()
    {
        if(instance == null)
        {
            instance = new DvdContainer();
        }
        return instance;
    }
    /**
       Tilføre et dvd object til arraylisten
       */
    public void addDVD(DVD dvd)
    {
        myDvdContainer.add(dvd);
    }
    public void findDvd(String title)
    {
        boolean found = false;
        for(int i = 0; i < myDvdContainer.size(); i++)
        {
            if(myDvdContainer.get(i).getTitle().equals(title))
            {
                System.out.println("Title " + myDvdContainer.get(i).getTitle());
                System.out.println("Barcode " + myDvdContainer.get(i).getBarcode());
                System.out.println("artist " + myDvdContainer.get(i).getartist());
                System.out.println("publicationDate " + myDvdContainer.get(i).getpublicationDate());
                found = true;
                //return  myDvdContainer.get(i);
            }           
        }
        if(!found)
        {
            System.out.println("no match found");            
        }
        //return null;
    }
    public DVD findDvdforloan(String title)
    {
        boolean found = false;
        for(int i = 0; i < myDvdContainer.size(); i++)
        {
            if(myDvdContainer.get(i).getTitle().equals(title))
            {
                found = true;
                if(myDvdContainer.get(i).getCopys().size() > 0)
                {
                  return myDvdContainer.get(i);    
                }
                else{
                    System.out.println("there is not copys home");
                    return null;
                }
            }           
        }
        if(!found)
        {
            System.out.println("no match found");
            return null;                     
        }
        return null;
    }
    
    public void updateDVD(String title)
    {
        Scanner keyboard = new Scanner(System.in);
         boolean found = false;
        for(int i = 0; i < myDvdContainer.size(); i++)
        {
            if(myDvdContainer.get(i).getTitle().equals(title))
            {
                System.out.println("enter title");
                 String newtitle = keyboard.next();
                myDvdContainer.get(i).setTitle(newtitle);
                System.out.println("enter barcode");
                int barcode = keyboard.nextInt(); 
                myDvdContainer.get(i).setBarcode(barcode);                
                System.out.println("enter artist ");
                String artist = keyboard.next();
                myDvdContainer.get(i).setartist(artist);
                System.out.println("enter publicationDate ");
                String publicationDate = keyboard.next();
                myDvdContainer.get(i).setpublicationDate(publicationDate);
                System.out.println("want to add copys (1) yes --- (2) no");
               int copychoos = keyboard.nextInt();
               if(copychoos == 1)
               {
                 System.out.println("how many copy?");
                 int amount = keyboard.nextInt();
                 System.out.println("enter purchaseDate");
                 String purchaseDate = keyboard.next();
                 System.out.println("enter purchasePrice");
                 double purchasePrice = keyboard.nextInt();
                 for(int x = 0; x < amount; x++)
                 {
                     int serialNumber = x+1;
                     Copy copy = new Copy(serialNumber, purchaseDate, purchasePrice);
                     myDvdContainer.get(i).addCopy(copy);
                 }
                }
                found = true;
            }           
        }
        if(!found)
        {
            System.out.println("no match found");
        }
    }
    public void removeDvd(String title)
    {
        for(int i = 0; i < myDvdContainer.size(); i++)
        {
            if(myDvdContainer.get(i).getTitle().equals(title))
            {
                myDvdContainer.remove(i);
            }
        }
    }
}
